package com.pig4cloud.mall.order.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.order.entity.CartItem;
import com.pig4cloud.mall.order.service.CartItemService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 购物车表
 *
 * @author fangyu
 * @date 2020-12-25 17:48:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cartitem")
@Api(value = "cartitem", tags = "购物车表管理")
public class CartItemController {

	private final CartItemService cartItemService;

	/**
	 * 分页查询
	 *
	 * @param page     分页对象
	 * @param cartItem 购物车表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('order_cartitem_get')")
	public R getCartItemPage(Page page, CartItem cartItem) {
		return R.ok(cartItemService.page(page, Wrappers.query(cartItem)));
	}


	/**
	 * 通过id查询购物车表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('order_cartitem_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(cartItemService.getById(id));
	}

	/**
	 * 新增购物车表
	 *
	 * @param cartItem 购物车表
	 * @return R
	 */
	@ApiOperation(value = "新增购物车表", notes = "新增购物车表")
	@SysLog("新增购物车表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('order_cartitem_add')")
	public R save(@RequestBody CartItem cartItem) {
		return R.ok(cartItemService.save(cartItem));
	}

	/**
	 * 修改购物车表
	 *
	 * @param cartItem 购物车表
	 * @return R
	 */
	@ApiOperation(value = "修改购物车表", notes = "修改购物车表")
	@SysLog("修改购物车表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('order_cartitem_edit')")
	public R updateById(@RequestBody CartItem cartItem) {
		return R.ok(cartItemService.updateById(cartItem));
	}

	/**
	 * 通过id删除购物车表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除购物车表", notes = "通过id删除购物车表")
	@SysLog("通过id删除购物车表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('order_cartitem_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(cartItemService.removeById(id));
	}

}
